package com.example.goodpet.community.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.bumptech.glide.Glide;
import com.example.goodpet.R;
import com.example.goodpet.activity.LoginActivity;
import com.example.goodpet.activity.PetInfoActivity;
import com.example.goodpet.activity.TieziInfoActivity;
import com.example.goodpet.bean.ResponseBody;
import com.example.goodpet.bean.UserPetInfo;
import com.example.goodpet.community.bean.TieziImage;
import com.example.goodpet.community.bean.UserTieziInfo;
import com.example.goodpet.home.homeRecommend.bean.GoodPetImageData;
import com.example.goodpet.service.HomeService;
import com.example.goodpet.utils.Constants;
import com.example.goodpet.utils.MyApplication;
import com.example.goodpet.utils.RetrofitSingleton;
import com.example.goodpet.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class CommunityRecyclerViewAdapter extends DelegateAdapter.Adapter<CommunityRecyclerViewAdapter.ViewHolder> {
    private String TAG = this.getClass().getSimpleName();
    private final Context mContext;
    public List<UserTieziInfo> datas;
    private Retrofit retrofit;
    private HomeService homeService;
    private Resources resources;
    private Activity activity;
    private final int TIEZIINFO=110;
    public CommunityRecyclerViewAdapter(Context mContext, List<UserTieziInfo> infos,Activity activity) {
        this.mContext=mContext;
        this.datas=infos;
        this.activity=activity;

        resources = mContext.getResources();
    }

    @NonNull
    @Override
    public CommunityRecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_community, parent, false);
        return new CommunityRecyclerViewAdapter.ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull CommunityRecyclerViewAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        //根据位置得到对应的数据
        UserTieziInfo info = datas.get(position);
//        Log.e(TAG, "onBindViewHolder: "+position+","+info.isCollect() );

//        Log.e(TAG, "onBindViewHolder: "+info );


        holder.tv_nickname.setText(info.getNickname());
        holder.tv_text.setText(info.getContent());
        //                        根据info的数据把宠物图片的第一张加载到相应的imageView上
        Glide.with(mContext)
                .load(Constants.BASE_URL_IMAGE+info.getTieziImage().get(0))
                .into(holder.iv_community);

        //根据info的数据初始化发布者信息
        Glide.with(mContext)
                .load(Constants.BASE_URL_IMAGE+info.getPortrait())
                .into(holder.iv_portrait);
        holder.tv_nickname.setText(info.getNickname());

        //获取点赞的数量，将点赞数量加上去
        holder.like_number.setText(String.valueOf(info.getLikeNum()));

        //点赞了，改变图标
        if(info.isLike()){
            Drawable drawable = resources.getDrawable(R.drawable.like32_al);
            holder.iv_like.setImageDrawable(drawable);
        }else{
            Drawable drawable = resources.getDrawable(R.drawable.like32_gray);
            holder.iv_like.setImageDrawable(drawable);
        }
        //不点赞 无操作

        //初始化like点击事件
        holder.like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                retrofit=RetrofitSingleton.getRetrofitConstance();
                homeService=retrofit.create(HomeService.class);
                //记得更新数据库
                homeService.updateUserLike(MyApplication.user.getId(),info.getTieziId())
                        .enqueue(new Callback<ResponseBody<Boolean>>() {
                            @Override
                            public void onResponse(Call<ResponseBody<Boolean>> call, Response<ResponseBody<Boolean>> response) {
                                ResponseBody<Boolean> body1 = response.body();
                                if(body1.getCode()==200){
                                    Boolean data1 = body1.getData();
                                    //点赞成功
                                    if(data1){
                                        //本来点赞了，再次点击取消点赞

                                        if(info.isLike()){
                                            Drawable drawable = resources.getDrawable(R.drawable.like32_gray);
                                            holder.iv_like.setImageDrawable(drawable);
                                            info.setLikeNum(info.getLikeNum()-1);
                                        }else{
                                            //本来没点赞，再次点击点赞
                                            Drawable drawable = resources.getDrawable(R.drawable.like32_al);
                                            holder.iv_like.setImageDrawable(drawable);
                                            info.setLikeNum(info.getLikeNum()+1);
                                        }
                                        info.setLike(!info.isLike());
                                        //更新显示的数量
                                        holder.like_number.setText(String.valueOf(info.getLikeNum()));
                                    }else{
                                        UIUtils.toast("点赞失败",false);
                                    }
                                }
                            }

                            @Override
                            public void onFailure(Call<ResponseBody<Boolean>> call, Throwable t) {
                                UIUtils.toast("网络通信失败，点赞失败",false);

                            }
                        });



                }
            });

            /**
             * 设置点击跳转到帖子详情页面的监听
             */
            holder.ly_item_community.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, TieziInfoActivity.class);
                    intent.putExtra("userTieziInfo",info);
                    intent.putExtra("position",position);
                    activity.startActivityForResult(intent,TIEZIINFO);
//                    mContext.startActivity(intent);
                }
            });


    }



    @Override
    public int getItemCount() {
        return datas==null?0:datas.size();
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return null;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_community;
        TextView tv_text;
        TextView tv_nickname;
        TextView like_number;
        CircleImageView iv_portrait;
        LinearLayout ly_item_community;
        LinearLayout like;
        ImageView iv_like;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_community=itemView.findViewById(R.id.iv_community);
            tv_text=itemView.findViewById(R.id.tv_text);
            tv_nickname=itemView.findViewById(R.id.tv_nickname);
            like_number=itemView.findViewById(R.id.like_number);
            iv_portrait=itemView.findViewById(R.id.iv_portrait);
            ly_item_community=itemView.findViewById(R.id.ly_item_community);
            like=itemView.findViewById(R.id.like);
            iv_like=itemView.findViewById(R.id.iv_like);
        }
    }

}
